//获取应用实例
const app = getApp()

Page({

  data: {
    showList:false,
    tabs: ["岗位", "门店"],

    title:null,

    tabIndex:0,

    showFilter: false,

    showJobType:false,
    jobType:-1,  //工作类型

    showWorkDate: false,
    sWorkDate: -1, //选择工作日期
    sShift: -1, //选择班次

    welfareList: [], //选择福利待遇

    payTypeList: [], //结算筛选
    interviewTimeList: [] //可面试时间

  },


  //下来刷新
  onPullDownRefresh() {
    if(this.data.tabIndex == 0){
      const jobListView = this.selectComponent("#jobList");
      const data = this.getSelectData();
      jobListView.search(data, false);
    }else{
      const storeListView = this.selectComponent("#storeList");
      const data = this.getSelectStoreData();
      storeListView.getStoreList(data);
    }
  },

  //上拉加载
  onReachBottom() {
    if(this.data.tabIndex == 0){
      const jobListView = this.selectComponent("#jobList");
      const data = this.getSelectData();
      jobListView.search(data, true);
    }
  },


  selectTab(e){
    const tabIndex = e.detail.tabIndex;
    if(tabIndex == 0){
      this.setData({
        tabIndex:0
      })
      if(this.data.tabIndex == 0){
        const jobListView = this.selectComponent("#jobList");
        const data = this.getSelectData();
        jobListView.search(data, false);
      }
    }else{
      this.setData({
        showFilter: false,
        showWorkDate: false,
        showJobType: false,
        tabIndex:1
      })
      if(this.data.tabIndex == 1){
        const storeListView = this.selectComponent("#storeList");
        const data = this.getSelectStoreData();
        storeListView.getStoreList(data);
      }
    }
  },
  
  search(e){
    const val = e.detail.searchText;
    const showList = e.detail.showList;
    //点击取消时重置tabIndex防止选择门店点击取消错乱
    if(!showList){
      this.setData({
        tabIndex:0
      })
    }
    this.setData({
      showList:showList,
      title:val,
      jobType:-1,
      sWorkDate: -1,
      sShift: -1,
      welfareList: [],
      payTypeList: [],
      interviewTimeList: []
    })
    if(this.data.tabIndex == 0){
      if(this.data.showList && this.data.tabIndex == 0){
        const jobListView = this.selectComponent("#jobList");
        const data = this.getSelectData();
        jobListView.search(data, false);
      }
    }else{
      if(this.data.showList && this.data.tabIndex == 1){
        const storeListView = this.selectComponent("#storeList");
        const data = this.getSelectStoreData();
        storeListView.getStoreList(data);
      }
    }
    
  },


  showJobType(){
    const show = !this.data.showJobType;
    this.setData({
      showWorkDate: false,
      showJobType: show,
      showFilter: show
    })
  },


  showWorkTimeDialog() {
    const show = !this.data.showWorkDate;
    this.setData({
      showWorkDate: show,
      showJobType: false,
      showFilter: show
    })
  },


  //选择工作类型
  selectJobType(res){
    this.setData({
      jobType:res.detail.jobType
    });
    if(!res.detail.reset) {
      this.submitFilter();
    }
  }, 

  //选择工作时间
  selectWorkDateType(res) {
    this.setData({
      sWorkDate: res.detail.sWorkDate,
      sShift: res.detail.sShift,
    });
    if(!res.detail.reset) {
      this.submitFilter();
    }
  },

  //选择福利
  selectWelfare(){
    let that = this;
    wx.navigateTo({
      url: '/pages/main/index/select-welfare/select-welfare?welfareList=' + JSON.stringify(that.data.welfareList),
      events: {
        welfarePage: function (res) {
          that.setData({
            welfareList: res.data
          })
          that.submitFilter();
        }
      }
    })
  },


  //选择其他页面
  selectOther() {
    let that = this;
    wx.navigateTo({
      url: '/pages/main/index/select-other/select-other?from=searchPage&payTypeList=' + JSON.stringify(that.data.payTypeList)+'&interviewTimeList='+JSON.stringify(that.data.interviewTimeList),
      events: {
        searchPage: function (res) {
          that.setData({
            payTypeList: res.resultPayType,
            interviewTimeList: res.resultInterviewTime
          });
          that.submitFilter();
        }
      }
    })
  },

  submitFilter() {
    this.setData({
      showFilter: false,
      showWorkDate: false,
      showJobType: false,
    });
    const jobListView = this.selectComponent("#jobList");
    const data = this.getSelectData();
    jobListView.search(data, false);
  },


  getSelectData() {
    return {
      jobType: this.data.jobType == -1 ? null:this.data.jobType,
      lat: app.globalData.latitude,
      lng: app.globalData.longitude,
      wantWorkArea:app.globalData.addressTitle,
      shiftType: this.data.sShift == -1 ? null : this.data.sShift,
      workType: this.data.sWorkDate == -1 ? null : this.data.sWorkDate,
      welfareIds: this.data.welfareList,
      interviewDates: this.data.interviewTimeList,
      settlementTypes: this.data.payTypeList,
      title:this.data.title,
    }
  },

  getSelectStoreData(){
    return {
      lat: app.globalData.latitude,
      lng: app.globalData.longitude,
      wantWorkArea:app.globalData.addressTitle,
      title:this.data.title,
    }
  }

})